package com.esm.pdawms.activity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.esm.pdawms.R;
import com.esm.pdawms.database.manager.SystemCenter;
import com.esm.pdawms.model.Churukuhp;
import com.esm.pdawms.util.HttpRequest;

import java.io.IOException;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.interfaces.OnHttpResponseListener;
import zuo.biao.library.util.JSON;

public class JyhpnumActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "JyhpnumActivity";

    public static Intent createIntent(Context context) {
        return new Intent(context, JyhpnumActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jyhpnum);

        _rukuhp = SystemCenter.getInstance().rukuhp;
        _danbh = SystemCenter.getInstance().esmcode;

        initView();
        initData();
        initEvent();
    }

    Churukuhp _rukuhp;
    String _danbh;

    private EditText edit_hpname;
    private EditText edit_hpcode;
    private EditText edit_rukudan;
    private EditText edit_znum;
    private EditText edit_hegenum;
    private EditText edit_ph;

    private EditText edit_num1;
    private EditText edit_onenum1;

    private EditText edit_num2;
    private EditText edit_onenum2;
    private TextView tv_lsph;
    private TextView tv_ph;


    @Override
    public void initView() {
        edit_hpname = (EditText) findViewById(R.id.edit_hpname);
        edit_hpcode = (EditText) findViewById(R.id.edit_hpcode);
        edit_rukudan = (EditText) findViewById(R.id.edit_rukudan);
        edit_hegenum = (EditText) findViewById(R.id.edit_hegenum);
        edit_znum = (EditText) findViewById(R.id.edit_znum);
        edit_num1 = (EditText) findViewById(R.id.edit_num1);
        edit_onenum1 = (EditText) findViewById(R.id.edit_onenum1);
        edit_num2 = (EditText) findViewById(R.id.edit_num2);
        edit_onenum2 = (EditText) findViewById(R.id.edit_onenum2);
        edit_ph  = (EditText) findViewById(R.id.edit_ph);
        tv_lsph  = (TextView) findViewById(R.id.tv_lsph);
        tv_ph  = (TextView) findViewById(R.id.tv_ph);
    }

    int _type = 0;
    @Override
    public void initData() {
        edit_hpname.setText(_rukuhp.getHpname());
        edit_hpcode.setText(_rukuhp.getHpcode());
        edit_rukudan.setText(_danbh);
        edit_hegenum.setText(_rukuhp.getHegenum() + "");
        edit_znum.setText(_rukuhp.getNum() + "");
        tv_lsph.setText(_rukuhp.getLastph());
        tv_ph.setText(_rukuhp.getPh());


        try {
            _type = Integer.parseInt(_rukuhp.getType());
        } catch (Exception e1) {

        }
        if (_rukuhp.getDxname().length() > 0 || _type > 1) {
            //大箱标 或非 0=半成品入库,1=成品缴库单
            findViewById(R.id.ll_ph).setVisibility(View.GONE);
            findViewById(R.id.ll_phnote).setVisibility(View.GONE);
            findViewById(R.id.ll_ph1).setVisibility(View.GONE);
            findViewById(R.id.ll_ph2).setVisibility(View.GONE);
        }

        String strPh = _rukuhp.getPh();//入库除了采购都要有批号
        if (strPh == null) {
            strPh = "";
        }
        if (_type == 0) {
             //BSN 半成品
            strPh = _rukuhp.getPh() + "##";
        } else if (_type == 1) {
            //SN  成品
            if (_rukuhp.getLastph().compareToIgnoreCase(strPh) > 0) {
                int phindex = Integer.parseInt(_rukuhp.getLastph().substring(2)) + 1;
                strPh = "SN"+ phindex;
            } else {
                strPh = _rukuhp.getPh();
            }
        }
        edit_ph.setText(strPh);
    }

    @Override
    public void initEvent() {
        findViewById(R.id.tv_qrrk).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_qrrk:
                onJyqu();
                break;
            default:
                break;
        }
    }

    //检验上传
    private void onJyqu() {
        float znum = 0;
        float hegenum = 0;
        int num1 = 0;
        float onenum1 = 0;
        int num2 = 0;
        float onenum2 = 0;
        try {
            znum = Float.parseFloat(edit_znum.getText().toString());
            hegenum = Float.parseFloat(edit_hegenum.getText().toString());
            num1 = Integer.parseInt(edit_num1.getText().toString());
            onenum1 = Float.parseFloat(edit_onenum1.getText().toString());
            num2 = Integer.parseInt(edit_num2.getText().toString());
            onenum2 = Float.parseFloat(edit_onenum2.getText().toString());
        } catch (Exception e1) {
            showShortToast("请输入数量！");
            return;
        }

        if (znum < hegenum) {
            showShortToast("合格数不能大于待检数！");
            return;
        }

        if (hegenum != (num1 * onenum1 + num2 * onenum2)) {
            showShortToast("AB组累计数量需等于合格数！");
            return;
        }

        String ph = edit_ph.getText().toString();
        if (_rukuhp.getDxname().length() > 0) {

        } else {
            if (_type <= 1 && (ph.length() < 10 || ph.contains("##"))) {
                showShortToast("请输入正确的批号！");
                return;
            }
        }

        showProgressDialog(R.string.loading);
        String hpcode = _rukuhp.getHpcode();
        String caozuo = SystemCenter.getInstance().caozuo;
        try {
            HttpRequest.createhprkcodeforruku(_rukuhp.getDxname(), _danbh, hpcode, caozuo, hegenum, num1, onenum1, num2, onenum2, ph, 0, new OnHttpResponseListener() {
                @Override
                public void onHttpResponse(int requestCode, String resultJson, Exception e) {
                    dismissProgressDialog();

                    JSONObject jsonObject = JSON.parseObject(resultJson);
                    int code = 0;
                    if (jsonObject != null) {
                        code = jsonObject.getInteger("code");
                    } else {
                        showShortToast(getString(R.string.wlwlj));
                        return;
                    }
                    if (code == 1) {
                        finish();
                    } else {
                        String msg = jsonObject.getString("msg");
                        showShortToast(msg);
                    }

                }
            });
        } catch (Exception e1) {
            showShortToast(getString(R.string.wlwlj));
            dismissProgressDialog();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}